package com.example.DataSource.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.DataSource.Mapper.CAMapper;
import com.example.DataSource.Service.CAService;
import com.example.DataSource.entity.consultantApplication;
import com.example.DataSource.entity.member;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CAServiceImpl extends ServiceImpl<CAMapper, consultantApplication> implements CAService {
    @Override
    @Transactional
    public IPage<consultantApplication> getCAPage(Page<consultantApplication> page) {
        QueryWrapper<consultantApplication> queryWrapper = new QueryWrapper<>();
        IPage<consultantApplication> CAPage = this.page(page,queryWrapper);
        List<consultantApplication> records = CAPage.getRecords();
//        需要获取用户昵称
        for (consultantApplication CA :
                records) {
            // 调用 getMemberName 方法获取 memberName
            String memberName = getMemberName(CA.getVip_id());
            // 将获取到的 member_name 设置到记录中
            CA.setMemberName(memberName);
        }
        return CAPage;
    }

    @Autowired
    private memberServiceImpl memberService;
    private String getMemberName(int vipId) {
//        根据会员Id查询会员昵称
        QueryWrapper<member> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",vipId);
        return memberService.list(queryWrapper).get(0).getNickname();
    }


    @Override
    public IPage<consultantApplication> getCAPageSearch(Page<consultantApplication> page, String searchInfo) {
        QueryWrapper<consultantApplication> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("content",searchInfo);
        IPage<consultantApplication> CAPage = this.page(page,queryWrapper);
        List<consultantApplication> records = CAPage.getRecords();
//        需要获取用户昵称
        for (consultantApplication CA :
                records) {
            // 调用 getMemberName 方法获取 memberName
            String memberName = getMemberName(CA.getVip_id());
            // 将获取到的 member_name 设置到记录中
            CA.setMemberName(memberName);
        }
        return CAPage;
    }


}
